package com.tairanchina.joat.groovy;

import com.tairanchina.joat.*;
import com.tairanchina.joat.exception.JoatInstallException;
import groovy.lang.GroovyClassLoader;
import org.apache.commons.codec.digest.DigestUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.NoSuchElementException;

/**
 * Created by 周靖捷 on 2016/10/11.
 */
public class GroovyPluginMeta extends PluginMeta {
    private GroovyClassLoader classLoader;

    public GroovyPluginMeta(String version, String signature, String pluginId, PluginInstallMode installMode, Class<? extends Plugin> pluginType) {
        super(version, signature, pluginId, installMode, pluginType);
    }

    public GroovyPluginMeta() {
        super();
        classLoader = new GroovyClassLoader();
    }

    public GroovyPluginMeta(String text, boolean isClassName) throws ClassNotFoundException {
        this();
        if (!isClassName) {
            pluginType = classLoader.parseClass(text);
        } else {
            pluginType = (Class<? extends Plugin>) classLoader.loadClass(text);
        }
        AnnotationPluginMeta annotationPluginMeta = pluginType.getAnnotation(AnnotationPluginMeta.class);
        this.version = annotationPluginMeta.version();
        this.pluginId = annotationPluginMeta.pluginId();
        this.installMode = annotationPluginMeta.installMode();
        this.signature = DigestUtils.md5Hex(text);
    }


    @Override
    public Plugin install(PluginManagerDe manager) throws JoatInstallException {
        try {
            if (installMode == PluginInstallMode.DECORATOR_MODE) {
                String decoratePluginId = getPluginType().getAnnotation(Decorator.class).value();
                DecoratorPlugin plugin = (DecoratorPlugin) getPluginType().getConstructor(Plugin.class).newInstance(manager.getPlugin(decoratePluginId).get());
                manager.decorate(decoratePluginId,plugin);
                return plugin;
            } else {
                Plugin plugin = getPluginType().newInstance();
                return plugin;
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchElementException e) {
            e.printStackTrace();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        throw new JoatInstallException("安装失败");
    }

    @Override
    public boolean uninstall(PluginManagerDe manager) throws JoatInstallException {
        if (installMode == PluginInstallMode.DECORATOR_MODE) {
            String decoratePluginId = getPluginType().getAnnotation(Decorator.class).value();
            manager.unDecorate(decoratePluginId, (DecoratorPlugin) manager.getPlugin(this.getPluginId()).get());
        }
        classLoader.clearCache();
        return true;
    }
}
